package route

import (
	"html/template"
	"io/fs"
	"net/http"

	"gitee.com/krio/ginc/example/core/lib/conf"
	"gitee.com/krio/ginc/example/router/serve/admin/web"
	"github.com/gin-gonic/gin"
)

// SetAdmin 嵌入的后台静态文件
// 基于layui：https://layui.dev/2.7/docs/
func SetAdmin(engine *gin.Engine) error {
	cssFS, err := fs.Sub(web.CssFS, "css")
	if err != nil {
		return err
	}
	imagesFS, err := fs.Sub(web.ImagesFS, "images")
	if err != nil {
		return err
	}
	jsFS, err := fs.Sub(web.JsFS, "js")
	if err != nil {
		return err
	}
	libFS, err := fs.Sub(web.LibFS, "lib")
	if err != nil {
		return err
	}
	pageFS, err := fs.Sub(web.PageFS, "page")
	if err != nil {
		return err
	}

	engine.StaticFS("/css", http.FS(cssFS))
	engine.StaticFS("/images", http.FS(imagesFS))
	engine.StaticFS("/js", http.FS(jsFS))
	engine.StaticFS("/lib", http.FS(libFS))
	engine.StaticFS("/page", http.FS(pageFS))

	tmpl, err := template.ParseFS(web.PageFS, "page/index/login.html")
	if err != nil {
		return err
	}
	engine.SetHTMLTemplate(tmpl)
	// 添加路由处理器并传递数据
	engine.GET("/admin/login", func(ctx *gin.Context) {
		data := map[string]interface{}{
			"AppName": conf.C.App.AppName,
			"AppEnv":  conf.C.App.AppEnv,
		}
		ctx.HTML(http.StatusOK, "login.html", data)
	})

	return nil
}
